package com.resou.reader.bookstore.freeandfinish;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.resou.reader.R;
import com.resou.reader.api.entry.FinishedBookBean;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.base.v.BaseActivity;
import com.resou.reader.bookdetail.BookDetailActivity;
import com.resou.reader.utils.NetworkUtils;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.log.DLog;
import com.resou.reader.view.DividerItemDecoration;
import com.resou.reader.view.TitleLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;

public class FinishBookActivity extends ResouBaseActivity<FinishBookPresenter> implements FinishBookContract.IFinishBookView {
    @BindView(R.id.mSmartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.title_layout)
    TitleLayout mTitleLayout;
    private FinishBookAdapter mAdapter;
    String type = "0";
    int page = 0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_finish_book;
    }

    @Override
    public void retryRefreshData() {
        page = 0;
        showLoading();
        mPresenter.quearFinishDataList(page);
    }

    @Override
    protected void initStatusBar() {

    }

    @Override
    protected void initPresenter() {
        mPresenter = new FinishBookPresenter(this);
    }

    @Override
    protected void initView() {
        if ("1".equals(getIntent().getStringExtra("action_type"))) {
            mTitleLayout.setCenterText(getString(R.string.fine_book));
        }
        showLoading();
        retryRefreshData();
        mAdapter = new FinishBookAdapter(R.layout.item_book_list, null);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (isNetwork()) {
                    page++;
                    mPresenter.quearFinishDataList(page);
                } else {
                    mAdapter.loadMoreFail();
                }
            }
        }, mRecyclerView);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                FinishedBookBean.BooksBean booksBean = (FinishedBookBean.BooksBean) adapter.getData().get(position);
                BookDetailActivity.startDetailActivity(FinishBookActivity.this, booksBean.getId());
            }
        });
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (isNetwork()) {
                    page = 0;
                    mPresenter.quearFinishDataList(page);
                } else {
                    showNetErrorView(R.id.rlContent);
                }
            }
        });
    }


    /**
     * @param context context
     * @param to      0代表到完结，1代表到免费
     */
    public static void startActivity(Context context, String to) {
        Intent intent = new Intent(context, FinishBookActivity.class);
        intent.putExtra("action_type", to);
        context.startActivity(intent);
    }


    @Override
    public void showDataList(List<FinishedBookBean.BooksBean> booksBeans) {
        if (page == 0) {
            mAdapter.setNewData(booksBeans);
        } else {
            mAdapter.addData(booksBeans);
        }
    }

    @Override
    public String getType() {
        return type;
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        return mAdapter;
    }

    @Override
    public SmartRefreshLayout getSmartRefreshLayout() {
        return mSmartRefreshLayout;
    }

    @Override
    public void initData() {

    }

    @Override
    public void showContent() {
        dissLoading();
    }

    @Override
    public void showProgress() {
        showLoading();
    }

    @Override
    public void showError(Throwable throwable) {
        ToastUtil.makeShortToast("网络异常");
        if (page == 0) {
            showNetErrorView(R.id.rlContent);
        }
    }

    @Override
    public void showEmpty() {

    }
}
